<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <section class="search-section">
      <div class="search-container">
        <el-input
          v-model="searchTitle"
          placeholder="搜索题目..."
          clearable
          size="large"
          @keyup.enter="handleSearch"
          class="search-input"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-select
          v-model="selectedSearchTags"
          multiple
          filterable
          collapse-tags
          collapse-tags-tooltip
          placeholder="选择标签..."
          size="large"
          class="tag-select"
          @visible-change="handleTagSelectVisible"
        >
          <el-option
            v-for="tag in filteredTagList"
            :key="tag.tagName"
            :label="`${tag.tagName} (${tag.tagCount})`"
            :value="tag.tagName"
          />
          <template #empty>
            <div v-if="loadingTags" style="padding: 1rem; text-align: center;">
              <el-icon class="is-loading"><Loading /></el-icon>
              <span style="margin-left: 0.5rem;">加载中...</span>
            </div>
            <div v-else style="padding: 1rem; text-align: center; color: #999;">暂无标签</div>
          </template>
        </el-select>
        <el-button 
          type="primary" 
          size="large"
          @click="handleSearch" 
          :icon="Search"
          style="min-width: 100px;"
        >
          查询
        </el-button>
      </div>
    </section>

    <!-- 主要内容区域 -->
    <section class="main-content">
      <!-- 左侧题目列表 -->
      <div class="questions-section">
        <el-skeleton v-if="loading" :rows="9" animated />
        <div v-else class="questions-grid">
        <div v-for="item in dataList" :key="item.id" class="question-card">
          <div class="card-header">
            <h3 class="card-title">{{ item.title }}</h3>
            <el-tag 
              :type="item.difficulty === 'EASY' ? 'success' : item.difficulty === 'MEDIUM' ? 'warning' : 'danger'"
              size="small"
            >
              {{ getDifficultyText(item.difficulty) }}
            </el-tag>
          </div>
          <div class="card-body">
            <div class="card-tags">
              <el-tag
                v-for="tag in getTagsArray(item.tags)"
                :key="tag"
                size="small"
                closable
                @close="removeTagFromItem(item, tag)"
                style="margin-right: 0.5rem; margin-bottom: 0.5rem;"
              >
                {{ tag }}
              </el-tag>
            </div>
            <div class="card-footer">
              <div class="card-source">
                <a :href="item.link" target="_blank" class="source-link" v-if="item.link">
                  面试鸭
                </a>
                <span v-else>{{ item.source }}</span>
              </div>
              <el-switch
                v-model="item.status"
                :active-value="'ACTIVE'"
                :inactive-value="'INACTIVE'"
                @change="toggleStatus(item)"
              />
            </div>
          </div>
          <div class="card-actions">
            <el-button size="small" @click="editItem(item)" :icon="Edit">编辑</el-button>
            <el-button size="small" type="primary" @click="generateMockExam(item)" :icon="MagicStick">
              生成模拟题
            </el-button>
            <el-button size="small" type="success" @click="startPractice(item)" :icon="VideoPlay">
              开始刷题
            </el-button>
            <el-button size="small" type="danger" @click="deleteItem(item.id)" :icon="Delete">删除</el-button>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <el-pagination
        background
        v-model:current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next"
        :hide-on-single-page="false"
        @current-change="goToPage"
        style="justify-content: center; margin-top: 2rem;"
      />
      </div>

    </section>

    <!-- 左下角固定操作按钮 -->
    <el-backtop :right="100" :bottom="100" />
    <div class="fixed-actions">
      <el-button
        type="primary"
        :icon="Plus"
        circle
        size="large"
        @click="showDialog = true"
        title="新增题目"
        style="width: 56px; height: 56px;"
      />
    </div>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="showDialog"
      :title="editingItem ? '编辑面试题' : '新增面试题'"
      width="500px"
      @close="showDialog = false"
    >
      <el-form :model="formData" label-width="80px" class="dialog-form">
        <el-form-item label="标题" required>
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="难度">
          <el-select v-model="formData.difficulty" placeholder="请选择难度" style="width: 100%">
            <el-option label="简单" value="EASY" />
            <el-option label="中等" value="MEDIUM" />
            <el-option label="困难" value="HARD" />
          </el-select>
        </el-form-item>
        <el-form-item label="标签">
          <el-select
            v-model="selectedTags"
            multiple
            filterable
            allow-create
            default-first-option
            :reserve-keyword="false"
            placeholder="可选 10 个标签，支持搜索"
            style="width: 100%"
            :max-collapse-tags="3"
          >
            <el-option
              v-for="tag in filteredTagOptions"
              :key="tag"
              :label="tag"
              :value="tag"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="链接">
          <el-input v-model="formData.link" placeholder="请输入链接" />
        </el-form-item>
        <el-form-item label="来源">
          <el-input v-model="formData.source" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="formData.status">
            <el-radio label="ACTIVE">有效</el-radio>
            <el-radio label="INACTIVE">无效</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="saveItem">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, onUnmounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import type { AxiosError } from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Edit, MagicStick, VideoPlay, Delete, Plus, Loading, ArrowDown } from '@element-plus/icons-vue'
import api from '@/utils/api'
import type { InterviewQuestion, PageRequest, TagVO } from '@/types/interview'

const router = useRouter()
const searchTitle = ref('')
const dataList = ref<InterviewQuestion[]>([])
const currentPage = ref(1)
const pageSize = ref(9)
const total = ref(0)
const loading = ref(false)
const showDialog = ref(false)
const editingItem = ref<InterviewQuestion | null>(null)

// 标签选择器相关（搜索用）
const allTagList = ref<TagVO[]>([])
const STORAGE_KEY = 'interview_question_search_tags'
const selectedSearchTags = ref<string[]>([])
const loadingTags = ref(false)

// 从 sessionStorage 恢复标签状态
const restoreTagsFromStorage = () => {
  try {
    const saved = sessionStorage.getItem(STORAGE_KEY)
    if (saved) {
      const tags = JSON.parse(saved)
      selectedSearchTags.value = tags
      return tags
    }
  } catch (e) {
    console.error('恢复标签状态失败:', e)
  }
  return []
}

// 保存标签状态到 sessionStorage
const saveTagsToStorage = (tags: string[]) => {
  try {
    sessionStorage.setItem(STORAGE_KEY, JSON.stringify(tags))
  } catch (e) {
    console.error('保存标签状态失败:', e)
  }
}

// 标签选择器相关（表单编辑用）
const selectedTags = ref<string[]>([])
const allTagOptions = ref<string[]>([
  'Java', 'Python', 'JavaScript', 'Vue', 'React', 'Spring', 'MySQL', 'Redis', 
  'Docker', 'Kubernetes', 'Git', 'Linux', '算法', '数据结构', 'CSS', 'HTML',
  'HTTP', 'TCP/IP', 'JVM', '并发编程', '设计模式', '微服务', '分布式'
])

const formData = ref<Partial<InterviewQuestion>>({
  id: '',
  title: '',
  difficulty: 'MEDIUM',
  tags: '',
  link: '',
  source: '',
  status: 'ACTIVE'
})

const totalPages = computed(() => Math.ceil(total.value / pageSize.value))

// 处理错误，如果是 403 错误则显示提示，其他错误显示通用提示
const handleError = (error: unknown, defaultMessage: string = '操作失败') => {
  const axiosError = error as AxiosError
  if (axiosError.response?.status === 403) {
    ElMessage.error('权限不足，您没有执行此操作的权限')
  } else {
    ElMessage.error(defaultMessage)
  }
  console.error(defaultMessage + ':', error)
}

const loadData = async () => {
  dataList.value = []
  loading.value = true
  try {
    const request: PageRequest = {
      pageNum: currentPage.value,
      pageSize: pageSize.value
    }
    
    const params: Record<string, string> = {}
    if (searchTitle.value) {
      params.title = searchTitle.value
    }
    if (selectedSearchTags.value.length > 0) {
      params.tags = selectedSearchTags.value.join(',')
    }
    
    const response = await api.post('/interview-questions/page', request, { params })
    if (response.data.code === 200) {
      const records = response.data.data.records || []
      dataList.value = records
      const respTotal = (response.data.data as any).total
      if (typeof respTotal === 'number' && respTotal > 0) {
        total.value = respTotal
      } else {
        // 乐观分页：当未提供 total 且刚好获取满一页数据时，临时认为还有下一页
        if (Array.isArray(records) && records.length === pageSize.value) {
          total.value = Math.max(total.value, currentPage.value * pageSize.value + 1)
        } else {
          total.value = Array.isArray(records) ? records.length : 0
        }
      }
      // 如果出现空页且当前不是第一页，则回退到上一页并修正 total
      if (Array.isArray(records) && records.length === 0 && currentPage.value > 1) {
        total.value = (currentPage.value - 1) * pageSize.value
        currentPage.value = currentPage.value - 1
        await loadData()
      }
    }
  } catch (error) {
    handleError(error, '加载数据失败')
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  // 保存标签状态（虽然 watch 会自动保存，但这里确保保存）
  saveTagsToStorage(selectedSearchTags.value)
  currentPage.value = 1
  loadData()
}

const goToPage = async (page: number) => {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page
    await loadData()
  }
}

const editItem = (item: InterviewQuestion) => {
  editingItem.value = item
  formData.value = { ...item }
  showDialog.value = true
  initTagsFromFormData()
}

const saveItem = async () => {
  try {
    const url = editingItem.value ? '/interview-questions' : '/interview-questions'
    const method = editingItem.value ? 'put' : 'post'
    
    const response = await api[method](url, formData.value)
    if (response.data.code === 200) {
      showDialog.value = false
      loadData()
    }
  } catch (error) {
    handleError(error, '保存失败')
  }
}

const deleteItem = async (id: string) => {
  try {
    await ElMessageBox.confirm('确定要删除吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
  } catch {
    return
  }
  
  try {
    const response = await api.delete('/interview-questions/batch', { data: [id] })
    if (response.data.code === 200) {
      loadData()
    }
  } catch (error) {
    handleError(error, '删除失败')
  }
}

const generateMockExam = async (item: InterviewQuestion) => {
  router.push({
    name: 'MockExam',
    query: { 
      id: item.id,
      title: item.title,
      difficulty: item.difficulty,
      tags: item.tags
    }
  })
}

const startPractice = async (item: InterviewQuestion) => {
  router.push({
    name: 'Practice',
    query: { 
      interviewQuestionId: item.id
    }
  })
}

const getDifficultyText = (difficulty: string) => {
  const map: Record<string, string> = {
    'EASY': '简单',
    'MEDIUM': '中等',
    'HARD': '困难'
  }
  return map[difficulty] || difficulty
}

const getTagsArray = (tags: string) => {
  if (!tags) return []
  return tags.split(',').map(t => t.trim()).filter(t => t)
}

const toggleStatus = async (item: InterviewQuestion) => {
  const newStatus = item.status === 'ACTIVE' ? 'INACTIVE' : 'ACTIVE'
  const oldStatus = item.status
  item.status = newStatus
  try {
    const response = await api.put('/interview-questions/status', null, {
      params: { id: item.id, status: newStatus }
    })
    if (response.data.code === 200) {
      loadData()
    } else {
      item.status = oldStatus
      ElMessage.error('更新状态失败: ' + (response.data.message || '未知错误'))
    }
  } catch (error) {
    item.status = oldStatus
    handleError(error, '更新状态失败')
  }
}

// 计算过滤后的标签选项
const filteredTagOptions = computed(() => {
  return allTagOptions.value.filter(tag => 
    !selectedTags.value.includes(tag)
  )
})

// 监听 selectedTags 变化，同步到 formData
watch(selectedTags, (newTags) => {
  if (newTags.length > 10) {
    ElMessage.warning('最多只能选择10个标签')
    selectedTags.value = newTags.slice(0, 10)
  }
  formData.value.tags = selectedTags.value.join(',')
}, { deep: true })

const initTagsFromFormData = () => {
  if (formData.value.tags) {
    selectedTags.value = formData.value.tags.split(',').map(t => t.trim()).filter(t => t)
  } else {
    selectedTags.value = []
  }
}

const removeTagFromItem = async (item: InterviewQuestion, tagToRemove: string) => {
  try {
    await ElMessageBox.confirm(`确定要删除标签"${tagToRemove}"吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
  } catch {
    return
  }
  
  try {
    const tagsArray = item.tags.split(',').map(t => t.trim()).filter(t => t && t !== tagToRemove)
    item.tags = tagsArray.join(',')
    
    const response = await api.put('/interview-questions', item)
    if (response.data.code === 200) {
      loadData()
    }
  } catch (error) {
    handleError(error, '删除标签失败')
  }
}

onMounted(() => {
  // 恢复标签状态
  restoreTagsFromStorage()
  // 恢复标签状态后自动加载数据
  loadData()
})

const loadTagList = async () => {
  loadingTags.value = true
  try {
    const request: PageRequest = {
      pageNum: 1,
      pageSize: 1000
    }
    const response = await api.post('/interview-questions/tags/page', request)
    if (response.data.code === 200) {
      allTagList.value = response.data.data.records
    }
  } catch (error) {
    // 加载标签列表失败时，不显示错误提示（因为这是可选功能）
    console.error('加载标签列表失败:', error)
  } finally {
    loadingTags.value = false
  }
}

// 计算过滤后的标签列表（el-select 会自动处理搜索过滤）
const filteredTagList = computed(() => {
  return allTagList.value
})

const handleTagSelectVisible = (visible: boolean) => {
  if (visible && allTagList.value.length === 0) {
    loadTagList()
  }
}

// 监听标签变化，自动保存到 sessionStorage
watch(selectedSearchTags, (newTags) => {
  saveTagsToStorage(newTags)
}, { deep: true, immediate: false })
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.app-container {
  min-height: 100vh;
  background: #f5f7fa;
}

/* 搜索区域 */
.search-section {
  padding: 1rem 0;
  background: transparent;
}

.search-container {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  align-items: center;
  background: white;
  border: 1px solid #e5e5e5;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  padding: 1rem;
}

.user-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: inline-flex;
  align-items: center;
}

.search-input {
  flex: 2;
  min-width: 250px;
}

.tag-select {
  flex: 2;
  min-width: 100px;
  max-width: 33.33%;
}

/* 题目区域 */
.main-content {
  padding: 2rem;
  max-width: 1600px;
  margin: 0 auto;
}

.questions-section {
  flex: 1;
  padding: 0;
  background: transparent;
}

.section-header {
  text-align: center;
  margin-bottom: 3rem;
}

.section-title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #333;
  margin-bottom: 0.5rem;
}

.section-subtitle {
  font-size: 1.2rem;
  color: #666;
}

.questions-grid {
  max-width: 1400px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 2rem;
}

.question-card {
  background: white;
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  border: 1px solid #e5e5e5;
  display: flex;
  flex-direction: column;
  min-height: 200px;
}

.question-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
  gap: 1rem;
}

.card-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #333;
  flex: 1;
  line-height: 1.5;
}

.card-body {
  margin-bottom: 1rem;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 1rem;
  border-top: none;
}

.card-source {
  font-size: 0.875rem;
  color: #666;
}

.source-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.2s ease;
}

.source-link:hover {
  color: #764ba2;
}

.card-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: auto;
  padding-top: 1rem;
}

.btn-card {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-edit {
  background: #e3f2fd;
  color: #1976d2;
}

.btn-edit:hover {
  background: #bbdefb;
}

.btn-delete {
  background: #ffebee;
  color: #d32f2f;
}

.btn-delete:hover {
  background: #ffcdd2;
}

/* 左下角固定操作按钮 */
.fixed-actions {
  position: fixed;
  left: 2rem;
  bottom: 2rem;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.action-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: white;
  color: #606266;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  padding: 0;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  color: #303133;
}

.action-btn:active {
  transform: translateY(0);
}

.action-btn svg {
  width: 20px;
  height: 20px;
}

.btn-ai {
  background: linear-gradient(135deg, #5A73FE 0%, #B49AFF 100%);
  color: white;
}

.btn-ai:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(90, 115, 254, 0.4);
}

.btn-practice {
  background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
  color: white;
}

.btn-practice:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(103, 194, 58, 0.4);
}

.btn-ai__icon-wrapper {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-ai__icon {
  width: 18px;
  height: 18px;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.1));
}

/* 标签样式 */
.tag {
  display: inline-block;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  background-color: #e5e5e5;
}

.tag-difficulty.tag-EASY,
.tag-difficulty.tag-简单 {
  background: linear-gradient(135deg, #e0f2fe 0%, #bae6fd 100%);
  color: #0369a1;
  border: none;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tag-difficulty.tag-MEDIUM,
.tag-difficulty.tag-中等 {
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
  color: #92400e;
  border: none;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tag-difficulty.tag-HARD,
.tag-difficulty.tag-困难 {
  background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
  color: #991b1b;
  border: none;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tag-wrapper {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  font-size: 0.75rem;
}

.tag-delete-icon {
  cursor: pointer;
  font-weight: bold;
  font-size: 1rem;
  margin-left: 0.25rem;
  opacity: 0.6;
  transition: all 0.2s;
}

.tag-delete-icon:hover {
  opacity: 1;
  color: #f56c6c;
}

/* Switch开关 */
.switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #808080;
  transition: 0.3s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.3s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #666666;
}

input:checked + .slider:before {
  transform: translateX(24px);
}

/* 分页 */
.pagination {
  max-width: 1400px;
  margin: 1.5rem auto;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 1.5rem 2rem;
}

.btn-page {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  background: white;
  color: #667eea;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid #e0e0e0;
}

.btn-page:hover:not(:disabled) {
  background: #667eea;
  color: white;
}

.btn-page:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  font-size: 1rem;
  color: #666;
}

.goto-page {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.goto-input {
  width: 60px;
  padding: 0.5rem;
  border: none;
  border-radius: 8px;
  text-align: center;
  font-size: 1rem;
}

.goto-input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.3);
}

/* 模态框 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.modal-content {
  background: white;
  padding: 2rem;
  border-radius: 16px;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.modal-content h3 {
  margin-top: 0;
  margin-bottom: 1.5rem;
  font-size: 1.5rem;
  font-weight: 600;
  color: #333;
  padding-bottom: 1rem;
  border-bottom: none;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #666;
  font-size: 0.875rem;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.2s ease;
  background: white;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

.form-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin-top: 1.5rem;
}

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn:not(.btn-primary) {
  background: #f5f5f5;
  color: #666;
}

.btn:not(.btn-primary):hover {
  background: #e0e0e0;
}

.loading {
  text-align: center;
  padding: 3rem;
  color: #667eea;
  font-size: 1.2rem;
  font-weight: 500;
}

/* 标签选择器 */
.tag-selector {
  position: relative;
  width: 100%;
}

.tag-selector-input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  min-height: 48px;
}

.tag-selector-input:hover {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.3);
}

.placeholder {
  color: #999;
  font-size: 0.875rem;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  flex: 1;
}

.selected-tag {
  display: inline-flex;
  align-items: center;
  padding: 0.4rem 0.8rem;
  background: #667eea;
  color: white;
  border-radius: 20px;
  font-size: 0.75rem;
}

.tag-remove {
  margin-left: 0.5rem;
  cursor: pointer;
  font-weight: bold;
}

.tag-remove:hover {
  opacity: 0.8;
}

.tag-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: none;
  border-radius: 8px;
  margin-top: 0.5rem;
  z-index: 1000;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.tag-search-input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: none;
  border-radius: 8px 8px 0 0;
  font-size: 0.875rem;
}

.tag-search-input:focus {
  outline: none;
}

.tag-options {
  max-height: 200px;
  overflow-y: auto;
}

.tag-option {
  padding: 0.75rem 1rem;
  cursor: pointer;
  font-size: 0.875rem;
  transition: background-color 0.2s;
}

.tag-option:hover {
  background-color: #f5f7fa;
}

.no-tags {
  padding: 1rem;
  text-align: center;
  color: #999;
  font-size: 0.875rem;
}

/* 对话框表单样式 */
.dialog-form :deep(.el-form-item) {
  margin-bottom: 20px;
}

.dialog-form :deep(.el-form-item:last-child) {
  margin-bottom: 0;
}


/* 响应式设计 */
@media (max-width: 768px) {
  .search-section {
    padding: 1rem;
  }

  .main-content {
    padding: 1rem;
  }

  .fixed-actions {
    left: 1rem;
    bottom: 1rem;
  }

  .questions-grid {
    grid-template-columns: 1fr;
  }
  
  .search-container {
    flex-direction: column;
  }

  .search-input {
    width: 100%;
    min-width: 100%;
    margin-left: 0;
    flex: none;
  }

  .tag-select {
    width: 100%;
    min-width: 100%;
    max-width: 100%;
    flex: none;
  }
}
</style>

<style>
.tag {
  font-size: 0.75rem;
  background-color: #e5e5e5;
}

/* 按钮内边距样式 */
.el-button {
  padding: 8px 15px !important;
}

.el-button--small {
  padding: 5px 11px !important;
}

.el-button--large {
  padding: 12px 19px !important;
}

.el-button.is-circle {
  padding: 0 !important;
}

/* 下拉列表选项左边距 */
.el-select-dropdown__item {
  padding-left: 20px !important;
}
</style>
